package com.neusoft.hifly.core.basic.service.impl;

import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.hifly.commons.database.DBUtil;
import com.neusoft.hifly.commons.exception.SystemErrorException;
import com.neusoft.hifly.commons.id.IdWorker;
import com.neusoft.hifly.commons.lang.StringConstants;
import com.neusoft.hifly.core.log.Log;

/**
 * Title: 核心类库
 * <p>
 * Description: 平台公共Service
 * <p>
 * Copyright: 2013
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
public class BaseServiceImpl {
	@Autowired(required = false)
	protected IdWorker idWorker;

	/**
	 * 拼接可IN的id条件
	 *
	 * @param ids
	 *            源ID
	 * @return 可以在SQL中使用IN的ID
	 */
	protected String[] makeInIds(final String ids) {
		if (StringUtils.isEmpty(ids)) {
			return null;
		}
		return ids.split(StringConstants.STRING_SPLIT_FLAG_3);
	}

	/**
	 * 查询条件
	 *
	 * @param property
	 *            参数
	 * @param queryWrapper
	 *            条件
	 * @param isAsc
	 *            是否升序
	 * @param orderColumns
	 *            排序字段
	 */
	@SuppressWarnings({ "rawtypes" })
	protected void createSql(final Map<String, Object> property, final QueryWrapper<?> queryWrapper,
			final boolean isAsc, final String... orderColumns) {
		final boolean isSetOrder = false;
		if (property != null) {

			Boolean isLike = (Boolean) property.get("isLike");
			if (isLike == null) {
				isLike = false;
			}

			final Iterator<?> iter = property.entrySet().iterator();
			while (iter.hasNext()) {
				final Map.Entry entry = (Map.Entry) iter.next();
				final String key = (String) entry.getKey();
				if (!"isLike".equals(key)) {
					if (isLike) {
						queryWrapper.like(key, entry.getValue());
					} else {
						queryWrapper.eq(key, entry.getValue());
					}
				}
			}

		}

		// 默认排序
		if (orderColumns != null && !isSetOrder) {
			if (isAsc) {
				queryWrapper.orderByAsc(orderColumns);
			} else {
				queryWrapper.orderByDesc(orderColumns);
			}
		}
	}

	/**
	 * 查询条件
	 *
	 * @param property
	 *            参数
	 * @param queryWrapper
	 *            条件
	 * @param orderColumns
	 *            排序字段
	 */
	protected void createSql(final Map<String, Object> property, final QueryWrapper<?> queryWrapper,
			final String... orderColumns) {
		createSql(property, queryWrapper, false, orderColumns);
	}

	/**
	 * 测试数据库连接
	 *
	 * @param dbType
	 *            数据库类型
	 * @param dbUrl
	 *            数据库地址
	 * @param dbUser
	 *            数据库登录名
	 * @param dbPwd
	 *            数据库密码
	 * @return 是否成功
	 */
	protected boolean testConnDB(final String dbType, final String dbUrl, final String dbUser, final String dbPwd) {
		if (StringUtils.isEmpty(dbType) || StringUtils.isEmpty(dbUrl) || StringUtils.isEmpty(dbUser)
				|| StringUtils.isEmpty(dbPwd)) {
			return false;
		}
		try {
			return DBUtil.getInstanse().testConn(dbType, dbUrl, dbUser, dbPwd, null);
		} catch (final SystemErrorException e) {
			Log.error(e.getErrorDesc());
		}
		return false;
	}

	/**
	 * 获取SQL文件名称
	 *
	 * @param dbType
	 *            数据库类型
	 * @return 文件名
	 */
	protected String sqlFileName(final String dbType) {
		return StringUtils.lowerCase(dbType) + ".sql";
	}
}
